﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WL_Store.BLL;
using WL_Store.Model;

namespace WL_Store.Web.subpage
{
    public partial class Feedback_edit : System.Web.UI.Page
    {
        FeedbackBLL bll = new FeedbackBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindList();

                int Id = Convert.ToInt32(Request.QueryString["Id"]);
                FeedbackModel model = bll.GetFeedback(Id);
                if (model!=null)
                {
                    txt_edit_number.Text = model.number.ToString();
                    txt_edit_name.Text = model.name.ToString();
                    txt_edit_count.Text = model.feedCount.ToString();
                    txt_edit_time.Value = model.feedTime.ToString();
                    ddl_edit_type.SelectedValue = model.typeId.ToString();
                    //model.name = txt_edit_name.Text;
                    //model.feedCount = txt_edit_count.Text;
                    //model.feedTime = Convert.ToDateTime(txt_edit_time.Value);
                    //model.typeId = Convert.ToInt32(ddl_edit_type.SelectedValue);
                }


            }
        }

        private void BindList()
        {
            DataTable dt = bll.GetTypes();
            
            ddl_edit_type.DataSource = dt;
            ddl_edit_type.DataTextField = "type";
            ddl_edit_type.DataValueField = "typeId";
            ddl_edit_type.DataBind();
        }

        protected void btn_edit_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                string number = txt_edit_number.Text.Trim();
                string name = txt_edit_name.Text.Trim();
                string count = txt_edit_count.Text.Trim();
                string feedtime = txt_edit_time.Value.Trim();
                int typeId = Convert.ToInt32(ddl_edit_type.SelectedValue);

                int Id = Convert.ToInt32(Request.QueryString["Id"]);

                FeedbackModel model = new FeedbackModel() { 
                
                    Id=Id,
                    number=number,
                    name=name,
                    feedCount=count,
                    feedTime=Convert.ToDateTime(feedtime),
                    typeId=typeId
                
                };
                if (bll.UpdateFeedback(model))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(),"alert", "<script>alert('更新成功！');location.href='Feedback.aspx'</script>");
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('更新失败！');location.href='Feedback.aspx'</script>");
                }
            }
        }
    }
}